﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SOM.AbstractDALFactory;
using SOM.Common;
using SOM.ConcreteDAL;
using SOM.ConcreteDALFactory;
using SOM.Model;
using SOM.Security;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Usuario usuario = SOM.Common.Common.RetornarUsuarioSessao(this);

        if (usuario == null)
        {
            FormsAuthentication.RedirectToLoginPage();
            return;
        }

        SOMAbstractDALFactory factory = new SOMConcreteDALFactory();
        TagDAL tagDal = factory.CreateTagDAL();
        dtlTopTags.DataSource = tagDal.GetTopTags(500);
        dtlTopTags.DataBind();

        ReclamacaoDAL reclamacaoDAL = factory.CreateReclamacaoDAL();
        grvTopReclamacoes.DataSource = reclamacaoDAL.GetTopReclamacoes(10);
        grvTopReclamacoes.DataBind();

        grvTopReclamacoes.Columns[0].Visible = false;

    }

    protected void dtlTopTags_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || 
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //Calcula o tamanho da fonte
            if (((Tag)e.Item.DataItem).tag_quantidade + 10 >= 25)
                e.Item.Font.Size = 25;
            else
                if (((Tag)e.Item.DataItem).tag_quantidade <= 10)
                    e.Item.Font.Size = 10;
                else
                    e.Item.Font.Size = ((Tag)e.Item.DataItem).tag_quantidade;
        }
    }
    protected void grvTopReclamacoes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string idReclamacao = SOMSecurity.Codificar(e.Row.Cells[0].Text);

            HyperLink hplVerMapa = (HyperLink)e.Row.FindControl("hplVerMapa");
            hplVerMapa.NavigateUrl =
                "javascript:AbrirPopup(\"Reclamacao/PopupReclamacaoLocalizacao.aspx?op=1&recId=" +
                idReclamacao + "\")";

            HyperLink hplReclamacaoDetalhes = (HyperLink)e.Row.FindControl("hypVisualizarReclamacao");
            hplReclamacaoDetalhes.NavigateUrl = "~/Reclamacao/ReclamacaoDetalhes.aspx?op=1&recId=" +
                                               idReclamacao;

            Label lblStatusReclamacao = (Label)e.Row.FindControl("lblStatusReclamacao");
            lblStatusReclamacao.Text = ((StatusDaReclamacao) int.Parse(lblStatusReclamacao.Text)).ToString();
            
        }
    }
}
